package com.android.monee.view.activity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.monee.binding.binder.Abstract1WBinder;
import com.android.monee.data.model.Budget.BudgetState;
import com.android.monee.domain.BudgetService;
import com.android.monee.view.model.BudgetDetails;
import com.android.monee.R;


public class BudgetDetailsFragment extends Fragment {

	public static String ARG_BUDGET_ID = "arg_budget_id";
	private BudgetDetails budget;
	private View root;
	private Menu menu;

	public BudgetDetailsFragment(){
		setHasOptionsMenu(true);
	}
	
	public BudgetDetails getBudget() {
		return budget;
	}
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		this.menu = menu;
		
		// Inflate the menu; this adds items to the action bar if it is present.
		inflater.inflate(R.menu.budgets_details, menu);
		
		updateMenu();
	}
	
	private void updateMenu(){
		if(this.budget.getState() == BudgetState.RUNNING){
			menu.findItem(R.id.budget_details_action_resume).setVisible(false);
			menu.findItem(R.id.budget_details_action_pause).setVisible(true);
		}
		else if(this.budget.getState() == BudgetState.PAUSED){
			menu.findItem(R.id.budget_details_action_resume).setVisible(true);
			menu.findItem(R.id.budget_details_action_pause).setVisible(false);
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.budget_details_action_pause:
			this.budget.setState(BudgetState.PAUSED);
			updateMenu();
			BudgetService.update(budget);
			return true;
		case R.id.budget_details_action_resume:
			this.budget.setState(BudgetState.RUNNING);
			updateMenu();
			BudgetService.update(budget);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		this.root = inflater.inflate(R.layout.fragment_budget_details,
				container, false);

		Bundle args = this.getArguments();
		int id = args.getInt(ARG_BUDGET_ID);
		this.budget = new BudgetDetails(BudgetService.findById(id),
				getResources());

		updateView();
		
		return root;

		//TODO : bug calcul nb mensualités 01/01/2012 à 01/12/2012 -> 1 mensualité + le nombre de mensualités dépend de la date de la paye, comment le prendre en compte ?
		// par défaut, nb mensualités = moi de début inclu jusqu'au moi echéance inclu
		//TODO prise en charge graphique d'un nouveau budget avec 0€ provisionné
		//TODO : option dans l'action bar : pause/resume, edit, delete
		//TODO : fragment dédié avec view modèle dédié pour modification
	}
	
	private void updateView(){
		
		TextView nameTV = (TextView) root
				.findViewById(R.id.budget_details_name);
		ImageView stateIV = (ImageView) root
				.findViewById(R.id.budget_details_icon_budget_state);
		TextView totalAmountTV = (TextView) root
				.findViewById(R.id.budget_details_total_amount);
		View notProvLblV = root.findViewById(R.id.budget_details_lbl_not_prov);
		View notProvGraphV = root
				.findViewById(R.id.budget_details_graph_not_prov);
		View provAmountLblTV = root
				.findViewById(R.id.budget_details_prov_amount_lbl);
		TextView provAmountTV = (TextView) root
				.findViewById(R.id.budget_details_prov_amount);
		TextView beginTV = (TextView) root
				.findViewById(R.id.budget_details_begin);
		TextView termTV = (TextView) root
				.findViewById(R.id.budget_details_term);
		TextView nbMensTV = (TextView) root
				.findViewById(R.id.budget_details_nb_mens);
		TextView mensAmountTV = (TextView) root
				.findViewById(R.id.budget_details_mens_amount);

		new Abstract1WBinder<BudgetDetails, BudgetDetails, BudgetDetails.StateIcon, ImageView, Drawable>(
				this.budget, this.budget.new StateIcon(), stateIV) {
			@Override
			public void updateTarget(ImageView target, Drawable value) {
				target.setImageDrawable(value);
			}
		};
		
		nameTV.setText(this.budget.getName());
		totalAmountTV.setText(this.budget.getTotalAmountLib());
		LayoutParams notProvParams = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, 0, this.budget.getNotProvAmount());
		notProvLblV.setLayoutParams(notProvParams);
		notProvGraphV.setLayoutParams(notProvParams);
		LayoutParams provAmountParams = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, 0, this.budget.getProvAmount());
		provAmountTV.setText(this.budget.getProvAmountLib());
		provAmountLblTV.setLayoutParams(provAmountParams);
		provAmountTV.setLayoutParams(provAmountParams);
		beginTV.setText(this.budget.getBeginLib());
		termTV.setText(this.budget.getTermLib());
		nbMensTV.setText(this.budget.getNbMensLib());
		mensAmountTV.setText(this.budget.getMensAmountLib());
	}

}
